package com.xiexuan.hearthstonecard;


import com.xiexuan.fragment.Card_fragment;
import com.xiexuan.fragment.Collect_fragment;
import com.xiexuan.fragment.News_fragment;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;

public class MainActivity extends FragmentActivity implements
		OnCheckedChangeListener {

	private Card_fragment card_fragment;
	private News_fragment news_fragment;
	private Collect_fragment collect_fragment;
	private RadioGroup rg_HSC;
	private FragmentManager fragmentmanager;
	private android.support.v4.app.FragmentTransaction fragmentTransaction;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		findView();

		fragmentmanager = this.getSupportFragmentManager();
		fragmentTransaction = fragmentmanager.beginTransaction();
		card_fragment = new Card_fragment();
		fragmentTransaction.add(R.id.fl_frame, card_fragment);
		fragmentTransaction.commit();
	}

	private void findView() {
		// TODO Auto-generated method stub
		rg_HSC = (RadioGroup) findViewById(R.id.rg_HSC);
		rg_HSC.setOnCheckedChangeListener(this);
	}

	@Override
	public void onCheckedChanged(RadioGroup group, int checkedId) {
		// TODO Auto-generated method stub
		fragmentmanager = this.getSupportFragmentManager();
		fragmentTransaction = fragmentmanager.beginTransaction();
		if (card_fragment != null) {
			fragmentTransaction.hide(card_fragment);
		}
		if (news_fragment != null) {
			fragmentTransaction.hide(news_fragment);
		}
		if (collect_fragment != null) {
			fragmentTransaction.hide(collect_fragment);
		}
		switch (checkedId) {
		case R.id.rb_card:
			if (card_fragment == null) {
				card_fragment = new Card_fragment();
				fragmentTransaction.add(R.id.fl_frame, card_fragment);
			} else
				fragmentTransaction.show(card_fragment);
			break;
		case R.id.rb_news:
			if (news_fragment == null) {
				news_fragment = new News_fragment();
				fragmentTransaction.add(R.id.fl_frame, news_fragment);
			} else
				fragmentTransaction.show(news_fragment);
			break;
		case R.id.rb_collect:
			if (collect_fragment == null) {
				collect_fragment = new Collect_fragment();
				fragmentTransaction.add(R.id.fl_frame, collect_fragment);
			} else
				fragmentTransaction.show(collect_fragment);
			break;
		default:
			break;
		}
		fragmentTransaction.commit();
	}
}
